package com.example.bill.repository;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.bill.entity.Memo;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MemoRepository extends SQLiteOpenHelper {
    public static final String TABLE_NAME = "memo";

    public MemoRepository(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE if not exists \"" + TABLE_NAME + "\" (" +
                "  \"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT," +
                "  \"name\" TEXT," +
                "  \"date\" TEXT," +
                " \"amount\" real DEFAULT 0,\n" +
                "  \"type\" integer" +
                ");");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public long insert(Memo memo) {
        ContentValues values = new ContentValues();
        if (memo.getId() != 0) {
            values.put("id", memo.getId());
        }
        values.put("name", memo.getName());
        if (memo.getDate() != null) {

            values.put("date", String.valueOf(memo.getDate().getTime()));
        }
        values.put("amount", memo.getAmount());
        values.put("type", memo.getType());
        return this.getWritableDatabase().insert(TABLE_NAME, null, values);
    }

    public List<Memo> queryByDate(int year, int month) {
        Date begin = toDate(year, month, 1, LocalTime.MIN);

        int lastDay = YearMonth.of(year, month).lengthOfMonth();

        Date end = toDate(year, month, lastDay, LocalTime.MAX);
        ArrayList<Memo> result = new ArrayList<>();
        try (Cursor cursor = getReadableDatabase().query(TABLE_NAME, new String[]{"id", "name", "date","amount","type"}, "date between ? and ?", new String[]{
                String.valueOf(begin.getTime()),
                String.valueOf(end.getTime())
        }, null, null, "date desc")) {


            while (cursor.moveToNext()) {
                Memo memo = new Memo();
                memo.setId(cursor.getInt(0));
                memo.setName(cursor.getString(1));
                memo.setDate(new Date(cursor.getLong(2)));
                memo.setAmount(cursor.getDouble(3));
                memo.setType(cursor.getInt(4));
                result.add(memo);
            }

        }
        return result;
    }

    private static Date toDate(int year, int month, int dayOfMonth, LocalTime time) {
        LocalDate date = LocalDate.of(year, month, dayOfMonth);
        LocalDateTime dateTime = LocalDateTime.of(date, time);
        return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
    }
}
